package jmine.tec.web.wicket.component.logininfo;

import static jmine.tec.web.wicket.component.logininfo.LoginInfoMessageCreator.DATE_PATTERN;
import static jmine.tec.web.wicket.component.logininfo.LoginInfoMessageCreator.FIRST_ACCESS;
import static jmine.tec.web.wicket.component.logininfo.LoginInfoMessageCreator.LAST_ACCESS;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import bancosys.tec.security.SecurityService;
import bancosys.tec.security.impl.dao.UserAccessDataDAO;
import bancosys.tec.security.impl.domain.UserAccessData;

/**
 * Informa o último login do usuário
 * 
 * @author bruno.ozahata
 * @created Nov 10, 2011
 */
public class LastLoginInfo implements LoginInfo {

    private UserAccessDataDAO userAccessDataDAO;

    private SecurityService securityService;

    /**
     * {@inheritDoc}
     */
    public String getMessage(String username) {
        DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN.create().getMessage());
        
        UserAccessData userData =
                this.userAccessDataDAO.findLastLoginByUsername(username, String.valueOf(this.securityService.getSystemId()));
        if (userData != null) {
            return LAST_ACCESS.create(dateFormat.format(userData.getLoginTimestamp())).getMessage();
        } else {
            return this.getFirstLoginMessage();
        }
    }

    /**
     * @return a mensagem de primeiro acesso.
     */
    private String getFirstLoginMessage() {
        return FIRST_ACCESS.create().getMessage();
    }

    /**
     * @param userAccessDataDAO the userAccessDataDAO to set
     */
    public void setUserAccessDataDAO(UserAccessDataDAO userAccessDataDAO) {
        this.userAccessDataDAO = userAccessDataDAO;
    }

    /**
     * @param securityService the securityService to set
     */
    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

}
